import vue from 'vue'
import toastComponent from './toast.vue'
import showLodingToast from './showLodingToast.vue'

// 组件构造器，构造出一个 vue组件实例
const ToastConstructor = vue.extend(toastComponent)
const showLodingToastConstructor = vue.extend(showLodingToast)

function showToast ({ text, type, duration = 2000 }) {
  const toastDom = new ToastConstructor({
    el: document.createElement('div'),
    data () {
      return {
        isShow: true, // 是否显示
        text: text, // 文本内容
        type: type // 类型
      }
    }
  })
  // 添加节点
  document.body.appendChild(toastDom.$el)
  // 过渡时间
  setTimeout(() => {
    toastDom.isShow = false
	document.body.removeChild(toastDom.$el)
  }, duration)
}
//注册加载组件
function showLoadingToast ({ showLoadingToastFlag }) {
  const toastDom = new showLodingToastConstructor({
    el: document.createElement('div'),
    data () {
      return {
        showLoadingToastFlag: showLoadingToastFlag, // 是否显示
      }
    }
  })
  toastDom.$el.id  = "showLoagId";
  // 添加节点
  document.body.appendChild(toastDom.$el)
}
//注册加载组件
function hideLoadingToast () {
  
var element = document.getElementById("showLoagId");
element.parentNode.removeChild(element);
}
// 全局注册
function registryToast () {
  vue.prototype.$toast = showToast
}
function registryLoadingToast () {
  vue.prototype.$showLoading = showLoadingToast
}
function registryHideLoadingToast () {
  vue.prototype.$hideLoading = hideLoadingToast
}

export  {
	registryToast,registryLoadingToast,registryHideLoadingToast
}